package com.fpc.cn.springbootutil.Slf4jLog;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.core.joran.spi.JoranException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * @date 2025/4/29
 * @author fupengcheng
 * @describe 自定义日志拦截器配置
 */
@Configuration
public class LogbackConfig {

    @PostConstruct
    public void init() throws JoranException {
        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();

        // 创建并添加自定义的日志拦截器（Appender）
        LoggingInterceptorAppender interceptorAppender = new LoggingInterceptorAppender();

        // 设置最小拦截级别为 INFO
        interceptorAppender.setMinLevel(Level.INFO);
        // 设置只拦截 com.fpc.cn.springbootutil.user 包及其子包的日志
        interceptorAppender.setPackagePrefix("com.fpc.cn.springbootutil.user");
        //启动拦截器
        interceptorAppender.start();
        loggerContext.getLogger(Logger.ROOT_LOGGER_NAME).addAppender(interceptorAppender);
    }
}